<?php
//imports
require_once("User.php");
	require_once("Activity.php");
	require_once("Item.php");
	require_once("Report.php");
	require_once("Item.php");
	require_once("Report.php");
	require_once("User.php");
	
class User_DAO{
	//Variables
		private $id;
		private $ManagerUser_id;
		private $firstName;
		private $lastName;
		private $email;
		private $credentials;
		private $isAdmin;
		private $isFinanceAdmin;
		private $feedKey;
		private $createdDate;
		private $profileChangeKey;
		private $isActive;
		

	//Accessors
		
	public function getId(){
		return $this->id;
	}
	
	public function setId($p_id){
		$this->id=$p_id;
	}
		
	public function getManagerUser_id(){
		return $this->ManagerUser_id;
	}
	
	public function setManagerUser_id($p_ManagerUser_id){
		$this->ManagerUser_id=$p_ManagerUser_id;
	}
		
	public function getFirstName(){
		return $this->firstName;
	}
	
	public function setFirstName($p_firstName){
		$this->firstName=$p_firstName;
	}
		
	public function getLastName(){
		return $this->lastName;
	}
	
	public function setLastName($p_lastName){
		$this->lastName=$p_lastName;
	}
		
	public function getEmail(){
		return $this->email;
	}
	
	public function setEmail($p_email){
		$this->email=$p_email;
	}
		
	public function getCredentials(){
		return $this->credentials;
	}
	
	public function setCredentials($p_credentials){
		$this->credentials=$p_credentials;
	}
		
	public function getIsAdmin(){
		return $this->isAdmin;
	}
	
	public function setIsAdmin($p_isAdmin){
		$this->isAdmin=$p_isAdmin;
	}
		
	public function getIsFinanceAdmin(){
		return $this->isFinanceAdmin;
	}
	
	public function setIsFinanceAdmin($p_isFinanceAdmin){
		$this->isFinanceAdmin=$p_isFinanceAdmin;
	}
		
	public function getFeedKey(){
		return $this->feedKey;
	}
	
	public function setFeedKey($p_feedKey){
		$this->feedKey=$p_feedKey;
	}
		
	public function getCreatedDate(){
		return $this->createdDate;
	}
	
	public function setCreatedDate($p_createdDate){
		$this->createdDate=$p_createdDate;
	}
		
	public function getProfileChangeKey(){
		return $this->profileChangeKey;
	}
	
	public function setProfileChangeKey($p_profileChangeKey){
		$this->profileChangeKey=$p_profileChangeKey;
	}
		
	public function getIsActive(){
		return $this->isActive;
	}
	
	public function setIsActive($p_isActive){
		$this->isActive=$p_isActive;
	}
		
	
	//Accessors for foreignKeys
		public function getManagerUser_id_Ref(){
		return User::getFromPrimaryKey($this->getManagerUser_id());
	}
		
	
	//Method to get referenced objects
		public function getActivitys(){
		return Activity::getFromCustomCriteria("WHERE ResponsibleUser_id=:value",array("value" => $this->getId()));
	}
			public function getCreatingUser_items(){
		return Item::getFromCustomCriteria("WHERE CreatingUser_id=:value",array("value" => $this->getId()));
	}
			public function getUser_reports(){
		return Report::getFromCustomCriteria("WHERE User_id=:value",array("value" => $this->getId()));
	}
			public function getAllocatedUser_items(){
		return Item::getFromCustomCriteria("WHERE AllocatedUser_id=:value",array("value" => $this->getId()));
	}
			public function getValidatingUser_reports(){
		return Report::getFromCustomCriteria("WHERE ValidatingUser_id=:value",array("value" => $this->getId()));
	}
			public function getUsers(){
		return User::getFromCustomCriteria("WHERE ManagerUser_id=:value",array("value" => $this->getId()));
	}
			
	/**
	 * Attempt an INSERT if the primary key is empty, otherwise perform an UPDATE.
	 * @param boolean $p_forceCreation true if save must trigger a create (by-pass the automatic ID generation) 
	 */
	public function save($p_forceCreation=false){
		try {
			$l_dbh = new PDO(__DB_URL, __DB_USER, __DB_PASSWORD);
			
						$l_hasKey=$this->getId();
			if(!isset($l_hasKey) || $p_forceCreation){
				//Attempt an INSERT
				if(!$p_forceCreation){
					$this->setId(strval(uniqid("W-", true)) . "-" . strval(rand(100000,999999)));
				}
								$l_sth = $l_dbh->prepare("INSERT INTO user (id,ManagerUser_id,firstName,lastName,email,credentials,isAdmin,isFinanceAdmin,feedKey,createdDate,profileChangeKey,isActive) VALUES (:id,:ManagerUser_id,:firstName,:lastName,:email,:credentials,:isAdmin,:isFinanceAdmin,:feedKey,:createdDate,:profileChangeKey,:isActive)");
									$l_sth->bindParam(':id',$this->getId());
									$l_sth->bindParam(':ManagerUser_id',$this->getManagerUser_id());
									$l_sth->bindParam(':firstName',$this->getFirstName());
									$l_sth->bindParam(':lastName',$this->getLastName());
									$l_sth->bindParam(':email',$this->getEmail());
									$l_sth->bindParam(':credentials',$this->getCredentials());
									$l_sth->bindParam(':isAdmin',$this->getIsAdmin());
									$l_sth->bindParam(':isFinanceAdmin',$this->getIsFinanceAdmin());
									$l_sth->bindParam(':feedKey',$this->getFeedKey());
									$l_sth->bindParam(':createdDate',$this->getCreatedDate());
									$l_sth->bindParam(':profileChangeKey',$this->getProfileChangeKey());
									$l_sth->bindParam(':isActive',$this->getIsActive());
								if(!$l_sth->execute()){
					throw new PDOException(implode(" - ",$l_sth->errorInfo()));
				}
			}else{
				//Attempt an UPDATE
								$l_sth = $l_dbh->prepare("UPDATE user SET ManagerUser_id=:ManagerUser_id, firstName=:firstName, lastName=:lastName, email=:email, credentials=:credentials, isAdmin=:isAdmin, isFinanceAdmin=:isFinanceAdmin, feedKey=:feedKey, createdDate=:createdDate, profileChangeKey=:profileChangeKey, isActive=:isActive WHERE id=:id");
									$l_sth->bindParam(':id',$this->getId());
									$l_sth->bindParam(':ManagerUser_id',$this->getManagerUser_id());
									$l_sth->bindParam(':firstName',$this->getFirstName());
									$l_sth->bindParam(':lastName',$this->getLastName());
									$l_sth->bindParam(':email',$this->getEmail());
									$l_sth->bindParam(':credentials',$this->getCredentials());
									$l_sth->bindParam(':isAdmin',$this->getIsAdmin());
									$l_sth->bindParam(':isFinanceAdmin',$this->getIsFinanceAdmin());
									$l_sth->bindParam(':feedKey',$this->getFeedKey());
									$l_sth->bindParam(':createdDate',$this->getCreatedDate());
									$l_sth->bindParam(':profileChangeKey',$this->getProfileChangeKey());
									$l_sth->bindParam(':isActive',$this->getIsActive());
								if(!$l_sth->execute()){
					throw new PDOException(implode(" - ",$l_sth->errorInfo()));
				}				
			}
			
			$l_dbh = null;
		} catch (PDOException $e) {
			print "Error ! : " . $e->getMessage() . "<br/>";
			die();
		}
	}
	
	/**
	 * Delete the current record from the database
	 */
	public function delete(){
		try {
			$l_dbh = new PDO(__DB_URL, __DB_USER, __DB_PASSWORD);
						$l_sth = $l_dbh->prepare("DELETE FROM user WHERE id=:value");
			$l_sth->bindParam(':value', $this->getId());
			if(!$l_sth->execute()){
				throw new PDOException(implode(" - ",$l_sth->errorInfo()));
			}
			//Set the primaryKey to null so that the object might be saved again (=created again)
			$this->setId(null);
		} catch (PDOException $e) {
			print "Error ! : " . $e->getMessage() . "<br/>";
			die();
		}	
	}
	
	
	/**
	 * Return all the objects from the table
	 */
	public static function getAll(){
		try {
			$l_results=array();
			$l_dbh = new PDO(__DB_URL, __DB_USER, __DB_PASSWORD);
			foreach ($l_dbh->query("SELECT * FROM user") as $l_row) {
				$l_object=new User();
								$l_object->setId($l_row['id']);
								$l_object->setManagerUser_id($l_row['ManagerUser_id']);
								$l_object->setFirstName($l_row['firstName']);
								$l_object->setLastName($l_row['lastName']);
								$l_object->setEmail($l_row['email']);
								$l_object->setCredentials($l_row['credentials']);
								$l_object->setIsAdmin($l_row['isAdmin']);
								$l_object->setIsFinanceAdmin($l_row['isFinanceAdmin']);
								$l_object->setFeedKey($l_row['feedKey']);
								$l_object->setCreatedDate($l_row['createdDate']);
								$l_object->setProfileChangeKey($l_row['profileChangeKey']);
								$l_object->setIsActive($l_row['isActive']);
								$l_results[$l_row['id']]=$l_object;
			}
			$l_dbh = null;
			return $l_results;
		} catch (PDOException $e) {
			print "Error ! : " . $e->getMessage() . "<br/>";
			die();
		}
	}
	
	/**
	 * Return the object identified by the specified key
	 * @param unknown_type $p_id the primary key value
	 */
	public static function getFromPrimaryKey($p_id){
		try {
			$l_dbh = new PDO(__DB_URL, __DB_USER, __DB_PASSWORD);
			$l_sth = $l_dbh->prepare("SELECT * FROM user WHERE id=:value");
			$l_sth->bindParam(':value', $p_id);
			if(!$l_sth->execute()){
				throw new PDOException(implode(" - ",$l_sth->errorInfo()));
			}
			
			$l_row=$l_sth->fetch(PDO::FETCH_ASSOC);
			if($l_row){
				$l_object=new User();
								$l_object->setId($l_row['id']);
								$l_object->setManagerUser_id($l_row['ManagerUser_id']);
								$l_object->setFirstName($l_row['firstName']);
								$l_object->setLastName($l_row['lastName']);
								$l_object->setEmail($l_row['email']);
								$l_object->setCredentials($l_row['credentials']);
								$l_object->setIsAdmin($l_row['isAdmin']);
								$l_object->setIsFinanceAdmin($l_row['isFinanceAdmin']);
								$l_object->setFeedKey($l_row['feedKey']);
								$l_object->setCreatedDate($l_row['createdDate']);
								$l_object->setProfileChangeKey($l_row['profileChangeKey']);
								$l_object->setIsActive($l_row['isActive']);
							}
			$l_dbh = null;
			return $l_object;
		} catch (PDOException $e) {
			print "Error ! : " . $e->getMessage() . "<br/>";
			die();
		}
	}
	
	/**
	 * Return the objects matching the specified SQL SEARCH CLAUSE<br>
	 * WARNING the search is based upon a preparedStatement.<br>
	 * @param string $p_selectionClause a SQL SEARCH CLAUSE (example: WHERE id=:idparam)
	 * @param array $p_arrayOfParameters an array of associations like (param to bind, bind value)
	 */
	public static function getFromCustomCriteria($p_selectionClause, $p_arrayOfParameters){
		try {
			$l_dbh = new PDO(__DB_URL, __DB_USER, __DB_PASSWORD);
			$l_sth = $l_dbh->prepare("SELECT * FROM user {$p_selectionClause}");
			
			$l_parameters=array_keys($p_arrayOfParameters);
			foreach($l_parameters as $l_parameter){
				$l_sth->bindParam($l_parameter, $p_arrayOfParameters[$l_parameter]);
			}
			
			if(!$l_sth->execute()){
				throw new PDOException(implode(" - ",$l_sth->errorInfo()));
			}
			
			$l_rows=$l_sth->fetchAll(PDO::FETCH_ASSOC);
			$l_results=array();
			
			foreach($l_rows as $l_row) {
				$l_object=new User();
								$l_object->setId($l_row['id']);
								$l_object->setManagerUser_id($l_row['ManagerUser_id']);
								$l_object->setFirstName($l_row['firstName']);
								$l_object->setLastName($l_row['lastName']);
								$l_object->setEmail($l_row['email']);
								$l_object->setCredentials($l_row['credentials']);
								$l_object->setIsAdmin($l_row['isAdmin']);
								$l_object->setIsFinanceAdmin($l_row['isFinanceAdmin']);
								$l_object->setFeedKey($l_row['feedKey']);
								$l_object->setCreatedDate($l_row['createdDate']);
								$l_object->setProfileChangeKey($l_row['profileChangeKey']);
								$l_object->setIsActive($l_row['isActive']);
								$l_results[$l_row['id']]=$l_object;
			}
			$l_dbh = null;
			return $l_results;
		} catch (PDOException $e) {
			print "Error ! : " . $e->getMessage() . "<br/>";
			die();
		}
	}
	
	
	/**
	 * Return the number of objects matching the specified SQL SEARCH CLAUSE<br>
	 * WARNING the search is based upon a preparedStatement.<br>
	 * @param string $p_selectionClause a SQL SEARCH CLAUSE (example: WHERE id=:idparam)
	 * @param array $p_arrayOfParameters an array of associations like (param to bind, bind value)
	 */
	public static function getCustomCount($p_selectionClause, $p_arrayOfParameters){
		try {
			$l_dbh = new PDO(__DB_URL, __DB_USER, __DB_PASSWORD);
			$l_sth = $l_dbh->prepare("SELECT COUNT(*) as count FROM user {$p_selectionClause}");
			
			$l_parameters=array_keys($p_arrayOfParameters);
			foreach($l_parameters as $l_parameter){
				$l_sth->bindParam($l_parameter, $p_arrayOfParameters[$l_parameter]);
			}
			
			if(!$l_sth->execute()){
				throw new PDOException(implode(" - ",$l_sth->errorInfo()));
			}
			
			$l_row=$l_sth->fetch(PDO::FETCH_ASSOC);
			$l_dbh = null;
			if($l_row){
				return $l_row["count"];
			}else{
				die();
			}
		} catch (PDOException $e) {
			print "Error ! : " . $e->getMessage() . "<br/>";
			die();
		}
	}
	
	
	/**
	 * Return the number of objects<br>
	 */
	public static function getCount(){
		return User::getCustomCount("",array()); 
	}
}
?>